<?
/**
 * VoiceOne Settings/Extensions/Voicemail
 *
 * Frontend to provide access to the extensions system in voiceone.
 * @package voiceone
 * @subpackage  settings_extensions
 */

  include ("../../../admin/config/cfg.php");
  global $cwlang;

  //================ POST & GET =================
  $alert=getAlert();
  //delete mailbox
  if((isset($_GET["del"]))&&($_GET["del"]!="")){
    $extension=$_GET["del"];
    //set asterisk database
    $r1=$asterisk->rmFromDB("DND",$extension);
    $r2=$asterisk->rmFromDB("CFU",$extension);
    $r3=$asterisk->rmFromDB("CFB",$extension);
    $r4=$asterisk->rmFromDB("CFNR",$extension);
    $ret=$r1*$r2*$r3*$r4;
    if ($ret>0) $alert="<div class=\"AlertGood\">".T_("Extension's Call Forward settings has been edited")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Extension's Call Forward settings hasn't been edited")."</div>";
    //go to main page
    goToMainPage($alert,"./");
  }
  if((isset($_GET["del-virtual"]))&&($_GET["del-virtual"]!="")){
    $virtual=$asterisk->getVirtualExtension($_GET["del-virtual"]);
    if((is_array($virtual))&&(is_array($virtual[0]))){
    	$extension=$virtual[0]["extension"];
    	//delete virtaul extension cf settings
    	$r1=$asterisk->rmFromDB("DND",$extension);
      $r2=$asterisk->rmFromDB("CFU",$extension);
      $r3=$asterisk->rmFromDB("CFB",$extension);
      $r4=$asterisk->rmFromDB("CFNR",$extension);
      $ret=$r1*$r2*$r3*$r4;
      //delete cf settings for each linked clients
    	if(($ret!=0)&&(is_array($virtual[0]["linked_clients"]))&&(count($virtual[0]["linked_clients"])>0)){
      	foreach($virtual[0]["linked_clients"] as $client_id){
        	$clientArr=$asterisk->getClient($client_id,0);
          if((is_array($clientArr))&&(is_array($clientArr[0]))){
          	$extension=$clientArr[0]["extension"];
          	//set asterisk database
            $r1=$asterisk->rmFromDB("DND",$extension);
            $r2=$asterisk->rmFromDB("CFU",$extension);
            $r3=$asterisk->rmFromDB("CFB",$extension);
            $r4=$asterisk->rmFromDB("CFNR",$extension);
            $ret=$r1*$r2*$r3*$r4*$ret;
            if($ret==0) break;
          }
        }
      }
    }else $ret=0;
    if ($ret>0) $alert="<div class=\"AlertGood\">".T_("Extension's Call Forward settings has been edited")."</div>";
    else $alert="<div class=\"AlertBad\">".T_("Extension's Call Forward settings hasn't been edited")."</div>";
    //go to main page
    goToMainPage($alert,"./");
  }
  //================ PAGE LOGIC ================
  //verify that asterisk is ON
  $status=$asterisk->pbxSwitch("status");
  if($status=="ON"){
    //get clients
    $clientArr=$asterisk->getClient(0,0);
    $virtualArr=$asterisk->getVirtualExtension(0);
    //get cf settings
    $cfArr=$asterisk->getClientsDBEntry(0);
    //build html
    $blacklist=array();
    if(is_array($virtualArr)){
      foreach($virtualArr as $virtual){
      	//add virtual extensions to client list
      	$entryArr[$virtual["extension"]]["id"]=$virtual["id"];
        $entryArr[$virtual["extension"]]["settings"]=$cfArr[$virtual["extension"]];
        $entryArr[$virtual["extension"]]["linked_clients"]=$virtual["linked_clients"];
        $blacklist=array_merge($blacklist,$virtual["linked_clients"]);
      }
    }
    if(is_array($clientArr)){
      foreach($clientArr as $client){
      	//skip extensions that are contained in a virtual extension
      	if((is_array($blacklist))&&(in_array($client["id"],$blacklist,true))){
        	continue;
        }
        //add single extensions to client list
        $entryArr[$client["extension"]]["id"]=$client["id"];
        $entryArr[$client["extension"]]["settings"]=$cfArr[$client["extension"]];
        $entryArr[$client["extension"]]["linked_clients"]=array();
      }
      foreach($entryArr as $extension=>$v){
        $id=$v["id"];
        $settings=$v["settings"];
        $html.="<tr>";
        $html.="  <td class=\"first small\"><span class=\"ext_list\"><a href=\"./edit/?id=".$id."\" style=\"cursor: pointer;\">".$extension."</a></span></td>";
        //inizialize DND
        if((isset($settings["DND"]))&&($settings["DND"]!="")){
          $html.="<td><img src=\"$relBasePath/public/img/ok.gif\" title=\"$title\" /></td>";
        }else $html.="<td>&nbsp;-&nbsp;</td>";
        //inizialize CFU
        if((isset($settings["CFU"]))&&($settings["CFU"]!="")){
          $html.="<td><span style=\"font-size: 10px; font-weight: bold;\">".$settings["CFU"]."</span></td>";
        }else $html.="<td>&nbsp;-&nbsp;</td>";
        //inizialize CFB
        if((isset($settings["CFB"]))&&($settings["CFB"]!="")){
          $html.="<td><span style=\"font-size: 10px; font-weight: bold;\">".$settings["CFB"]."</span></td>";
        }else $html.="<td>&nbsp;-&nbsp;</td>";
        //inizialize CFNR
        if((isset($settings["CFNR"]))&&($settings["CFNR"]!="")){
          $html.="<td><span style=\"font-size: 10px; font-weight: bold;\">".$settings["CFNR"]."</span></td>";
        }else $html.="<td>&nbsp;-&nbsp;</td>";
        //delete button
        if((is_array($v["linked_clients"]))&&(count($v["linked_clients"])>0)){
        	$html.="  <td><img style=\"cursor: pointer;\" src=\"$relBasePath/public/img/trash.gif\" onclick=\"if(confirm('".T_("Are you sure you want to deactivate all settings for this Extension?")."')) location.href='./?del-virtual=".$id."'; else return false;\" /></td>";
        }else $html.="  <td><img style=\"cursor: pointer;\" src=\"$relBasePath/public/img/trash.gif\" onclick=\"if(confirm('".T_("Are you sure you want to deactivate all settings for this Extension?")."')) location.href='./?del=".$extension."'; else return false;\" /></td>";
        $html.="</tr>";
      }
    }else{
      $html.="<tr>";
      $html.="  <td class=\"first\" colspan=\"6\"><span style=\"font-size: 10px; font-weight: bold;\">".T_("There are no defined extensions")."</span></td>";
      $html.="</tr>";
    }
  }else{
    $html.="<tr>";
    $html.="  <td class=\"first\">&nbsp;</td>";
    $html.="  <td colspan=\"4\"><span style=\"font-size: 10px; font-weight: bold;\">".T_("No information available when PBX is off")."</span></td>";
    $html.="  <td>&nbsp;</td>";
    $html.="</tr>";
  }

  //================ MAIN CONTENTS ================
  ob_start();
  ?>
    <div class="SectionHeader">
      <?=$alert?>
      <h1><?=T_("Call Forward")?></h1>
    </div>

    <table id="ext_table" cellpadding="0" cellspacing="0" width="100%">
  		<tr>
        <th class="small none">&nbsp;</th>
        <th class="first bordered" width="20%"><?=T_("DND")?></th>
        <th class="bordered" width="20%"><?=T_("CFU")?></th>
        <th class="bordered" width="20%"><?=T_("CFB")?></th>
        <th class="bordered" width="20%"><?=T_("CFNR")?></th>
        <th class="small none">&nbsp;</th>
      </tr>
      <?=$html?>
    </table>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start();
  ?>
    <div class="Sidebar">
      <?$sel="callforward";include("../sidebar.php");?>
      <h1><?=T_("Informations")?></h1>
    </div>
  <?
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================
  $smarty->assign("section", "extensions");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

